package com.pirestupppai.ui.activity;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;

import com.pirestupppai.R;
import com.pirestupppai.base.BaseActivity;
import com.pirestupppai.common.Constant;
import com.pirestupppai.widgets.view.TitleBar;
import com.pirestupppai.widgets.view.TupaiWebView;

import butterknife.BindView;

/**
 * 通用WebView
 *
 * @author: xiaoluo
 * @date: 2017-03-16 18:25
 */
public class WebActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.webview)
    TupaiWebView webview;

    private String mTitle;
    private String mUrl;
    private boolean getTitleFromWeb = true;  // 是否从网页获取标题

    @Override
    protected Context getActivityContext() {
        return this;
    }

    @Override
    protected int loadParentLayout() {
        return R.layout.activity_web;
    }

    @Override
    protected void initViews() {
        mTitle = getIntent().getStringExtra(Constant.IntentKey.WEBVIEW_TITLE);
        mUrl = getIntent().getStringExtra(Constant.IntentKey.WEBVIEW_URL);
        // 如果有传标题,不再从网页获取
        if (!TextUtils.isEmpty(mTitle)) {
            titleBar.setTitle(mTitle);
            getTitleFromWeb = false;
        }

        webview.loadUrl(mUrl);
    }

    @Override
    protected void initListeners() {
        // 是否从网页获取标题
        webview.setOnTitleListener(new TupaiWebView.OnTitleListener() {
            @Override
            public void onTitle(String title) {
                if (getTitleFromWeb) {
                    titleBar.setTitle(title);
                }
            }
        });
    }

    @Override
    protected void initLogic() {

    }

    @Override
    public void onClick(View v) {

    }

    /**
     * 解决webview关闭后视频仍有声音问题
     */
    @Override
    protected void onPause() {
        super.onPause();
        webview.onPause();
        webview.pauseTimers();
    }

    @Override
    protected void onResume() {
        super.onResume();
        webview.onResume();
        webview.resumeTimers();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        webview.loadUrl("");
    }
}
